package com.zhouyu;

import com.testimpot.ImportPkgCfg;
import com.zhouyu.service.FactoryService;
import com.zhouyu.service.StringToUserConvertor;
import com.zhouyu.service.StringToUserPropertyEditor;
import com.zhouyu.service.User;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.*;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.beans.PropertyEditor;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@ComponentScan(value = "com.zhouyu",
		excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = FactoryService.class)})
@Import(ImportPkgCfg.class)
@PropertySource("classpath:spring.properties")
public class AppConfig {

	/**
	 * 这里需要显式指定 beanName（或者方法名作为 beanName，必须是 messageSource ），否则会失败
	 *
	 * @return MessageSource
	 */
	@Bean
	public MessageSource messageSource() {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		// messages 要和 message_xx.properties 文件保持一致
		messageSource.setBasename("messages");
		return messageSource;
	}

//	@Bean
//	public JdbcTemplate jdbcTemplate() {
//		return new JdbcTemplate(dataSource());
//	}
//
//	@Bean
//	public PlatformTransactionManager transactionManager() {
//		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
//		transactionManager.setDataSource(dataSource());
//		return transactionManager;
//	}
//
//	@Bean
//	public DataSource dataSource() {
//		DriverManagerDataSource dataSource = new DriverManagerDataSource();
//		dataSource.setUrl("jdbc:mysql://mysql.test.baiwang-inner.com/yzt_driver?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai");
//		dataSource.setUsername("yzt_driver");
//		dataSource.setPassword("yzt_driver");
//		return dataSource;
//	}
//
//	@Bean
//	public SqlSessionFactory sqlSessionFactory() throws Exception {
//		SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
//		sessionFactoryBean.setDataSource(dataSource());
//		return sessionFactoryBean.getObject();
//	}
//
//	@Bean
//	public SqlSessionTemplate sqlSession() throws Exception {
//		return new SqlSessionTemplate(sqlSessionFactory());
//	}

	@Bean
	public ApplicationListener applicationListener() {

		return new ApplicationListener() {
			@Override
			public void onApplicationEvent(ApplicationEvent event) {
				System.out.println("接收到了一个事件：" + event.getSource());
			}
		};
	}

	/**
	 * 向 Spring 中注册 CustomEditorConfigurer，可以转换对象，实现变相注册 Bean
	 *
	 * @return CustomEditorConfigurer
	 */
	@Bean
	public CustomEditorConfigurer customEditorConfigurer() {
		CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer();
		Map<Class<?>, Class<? extends PropertyEditor>> propertyEditorMap = new HashMap<>();

		// 表示StringToUserPropertyEditor可以将String转化成User类型，在Spring源码中，
		// 如果发现当前对象是String，而需要的类型是User，就会使用该PropertyEditor来做类型转化
		propertyEditorMap.put(User.class, StringToUserPropertyEditor.class);
		customEditorConfigurer.setCustomEditors(propertyEditorMap);
		return customEditorConfigurer;
	}

	/**
	 * 向 Spring 中注册 StringToUserConvertor
	 *
	 * @return ConversionServiceFactoryBean
	 */
	@Bean
	public ConversionServiceFactoryBean conversionServiceFactoryBean() {


		ConversionServiceFactoryBean factoryBean = new ConversionServiceFactoryBean();
		factoryBean.setConverters(Collections.singleton(new StringToUserConvertor()));
		return factoryBean;
	}


}


